package cn.information.dao;


import cn.information.bean.Supplier;
import cn.information.utils.DButil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SupplierDao {
    public static List<Supplier> findAll() {
        Connection conn = DButil.getConnection ();
        String sql = "select * from Supplier_info";
        Statement stmt = null;
        List<Supplier> list =new ArrayList<>();
        try {
            stmt =conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while(rs.next()){
                Supplier supplier = new Supplier();
                supplier.setGysid(rs.getInt("gysid"));
                supplier.setGysname(rs.getString("gysname"));
                supplier.setGysphone(rs.getString("gysphone"));
                supplier.setAddress(rs.getString("address"));
                supplier.setSpname(rs.getString("spname"));
                list.add(supplier);
            }
        } catch(Exception e){
            e.printStackTrace();
        }
        finally{
            DButil.close(conn);
        }
        return list;
    }


    public boolean addSupplier(Supplier supplier) {
        Connection conn = DButil.getConnection ();
        String sql = "insert into Supplier_info(gysid,gysname,gysphone,address,spname) values(?,?,?,?,?)";
        PreparedStatement stmt = null;//try catch异常处理
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, supplier.getGysid());
            stmt.setString(2, supplier.getGysname());
            stmt.setString(3, supplier.getGysphone());
            stmt.setString(4, supplier.getAddress());
            stmt.setString(5, supplier.getSpname());
            if(stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean deleteSupplier(int id) {
        Connection conn = DButil.getConnection ();
        String sql = "delete from Supplier_info where gysid=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, id);
            if(stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }

    public boolean updateSupplier(Supplier supplier) {
        Connection conn = DButil.getConnection ();
        String sql = "update Supplier_info set gysname=?,gysphone=?,address=?,spname=? where gysid=?";
        PreparedStatement stmt = null;
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, supplier.getGysname());
            stmt.setString(2, supplier.getGysphone());
            stmt.setString(3, supplier.getAddress());
            stmt.setString(4, supplier.getSpname());
            stmt.setInt(5, supplier.getGysid());
            if(stmt.executeUpdate()>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DButil.close(conn);
        }
        return false;
    }
    public static Supplier findById(int gysid) {
        String sql = "SELECT * FROM supplier_info WHERE gysid = ?";
        try (Connection conn = DButil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, gysid);
            ResultSet rs = stmt.executeQuery();

            if (rs.next()) {
                Supplier supplier = new Supplier();
                supplier.setGysid(rs.getInt("gysid"));
                supplier.setGysname(rs.getString("gysname"));
                supplier.setGysphone(rs.getString("gysphone"));
                supplier.setAddress(rs.getString("address"));
                supplier.setSpname(rs.getString("spname"));
                return supplier;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        return null;
    }


}
